package com.digua.base.socket.request

import java.util.ArrayDeque
import java.util.Queue

class RequestFactory {

    companion object {

        private const val POLL_SIZE = 7
        private val BYTE_ARRAY_REQUEST_POOL: Queue<ByteArrayRequest> = ArrayDeque(POLL_SIZE)
        private val BYTE_BUFFER_REQUEST_POOL: Queue<ByteBufferRequest> = ArrayDeque(POLL_SIZE)
        private val STRING_REQUEST_POOL: Queue<StringRequest> = ArrayDeque(POLL_SIZE)
        private val PING_REQUEST_POOL: Queue<PingRequest> = ArrayDeque(POLL_SIZE)
        private val PONG_REQUEST_POOL: Queue<PongRequest> = ArrayDeque(POLL_SIZE)
        private val FRAME_DATA_REQUEST_POOL: Queue<FrameDataRequest> = ArrayDeque(POLL_SIZE)
        private val COLLECTION_FRAME_REQUEST_POOL: Queue<CollectionFrameDataRequest> = ArrayDeque(POLL_SIZE)

        fun createByteArrayRequest() = BYTE_ARRAY_REQUEST_POOL.poll() ?: ByteArrayRequest()

        fun createByteBufferRequest() = BYTE_BUFFER_REQUEST_POOL.poll() ?: ByteBufferRequest()

        fun createStringRequest() = STRING_REQUEST_POOL.poll() ?: StringRequest()

        fun createPingRequest() = PING_REQUEST_POOL.poll() ?: PingRequest()

        fun createPongRequest() = PONG_REQUEST_POOL.poll() ?: PongRequest()

        fun createFrameDataRequest() = FRAME_DATA_REQUEST_POOL.poll() ?: FrameDataRequest()

        fun createCollectionFrameRequest() = COLLECTION_FRAME_REQUEST_POOL.poll() ?: CollectionFrameDataRequest()

        fun releaseByteArrayRequest(request: ByteArrayRequest) = BYTE_ARRAY_REQUEST_POOL.offer(request)

        fun releaseByteBufferRequest(request: ByteBufferRequest) = BYTE_BUFFER_REQUEST_POOL.offer(request)

        fun releaseStringRequest(request: StringRequest) = STRING_REQUEST_POOL.offer(request)

        fun releasePingRequest(request: PingRequest) = PING_REQUEST_POOL.offer(request)

        fun releasePongRequest(request: PongRequest) = PONG_REQUEST_POOL.offer(request)

        fun releaseFrameDataRequest(request: FrameDataRequest) = FRAME_DATA_REQUEST_POOL.offer(request)

        fun releaseCollectionFrameRequest(request: CollectionFrameDataRequest) = COLLECTION_FRAME_REQUEST_POOL.offer(request)
    }
}